﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using HA.Data;

namespace HA.ModFlow.Application
{
   public  class Grid3dCollecionViewModel:MDTVIViewModel
    {

       public Grid3dCollecionViewModel(ProjectDataViewModel parent)
           : base(parent, false)
       {
           this._contextMenu = new System.Windows.Controls.ContextMenu();
           //MenuItem mi;

           //mi = new MenuItem();
           //mi.Header = "删除";
           //this._contextMenu.Items.Add(mi);

           //mi = new MenuItem();
           //mi.Header = "新建 Grid3D数据";
           //this._contextMenu.Items.Add(mi);


           this._imageSource = VMImageSource.Grid3dCollecion;

           this.LoadChildren();

       }


       public override string Desc
       {
           get
           {
               return "三维网格数据";
           }
       }



       protected override void LoadChildren()
       {
           Grid3dCollecion collecion = ProjectService.DefaultService.Project.Grid3dData;

           foreach (Grid3d item in collecion.Grids)
           {
               Grid3dViewModel gvm = new Grid3dViewModel(this,item);

               base.Children.Add(gvm);

           }


           collecion.CollectionChanged += new EventHandler<CollecionChangedEventArgs>(collecion_CollectionChanged);


         
       }

       void collecion_CollectionChanged(object sender, CollecionChangedEventArgs e)
       {
           switch (e.Type)
           {
               case CollectionChangedType.ADD:

                   Grid3dViewModel gvm = new Grid3dViewModel(this,(Grid3d)e.Child);

                   this.Children.Add(gvm);


                   break;
               case CollectionChangedType.REMOVE:

                   this.Children.RemoveAt(0);


                   break;
               case CollectionChangedType.MOVE:
                   break;
               case CollectionChangedType.REPLACE:

                   Grid3dViewModel gvmOld = (Grid3dViewModel)this.Children[0];

                   this.Children.Remove(gvmOld);

                    Grid3dViewModel gvmNew = new Grid3dViewModel(this,(Grid3d)e.Child);

                    this.Children.Add(gvmNew);






                   break;
               case CollectionChangedType.ITEMEDIT:
                   break;
               case CollectionChangedType.RESET:
                   break;
               default:
                   break;




           }


           ProjectDataViewModel parent = (ProjectDataViewModel)this.Parent;

           parent.CollectionView.Refresh();




       }



       //protected Grid3dViewModel GetChildViewModel()
       //{
 
       //}


       public Grid3dViewModel GetMainGrid3dVM()
       {
           if (this.Children.Count>0)
           {
               return (Grid3dViewModel)this.Children[0];
               
           }

           return null;
 
       }



       protected override void OnSelectedChanged()
       {

           if (this.IsSelected)
           {
             


               if (ProjectService.DefaultService.Project.Grid3dData.MainGrid!=null)
               {

                   string name = GisModule.DefaultModule.GridLayerId;

                   GisModule.DefaultModule.SetCurrentLayer(name);


                   //GisModule.DefaultModule.GisManager.InteractiveSystem.CurrentLayer = GisModule.DefaultModule.GisManager.LayerSystem.LayerCollection.GetLayer(ProjectSerivce.DefaultService.Project.Grid3dData.MainGrid.Key.ToString());


                   
               }

               UIModule.DefaultModule.RibbonV2ViewModel.ViewOpViewModel.ToolVM.CurrentTool = APPTOOLMODE.GRID3D;

            
           }


       }


    }
}
